package com.goods.business.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.converter.SupplierConverter;
import com.goods.business.mapper.SupplierMapper;
import com.goods.business.service.SupplierService;
import com.goods.common.model.business.Supplier;
import com.goods.common.response.ResponseBean;
import com.goods.common.vo.business.SupplierVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class SupplierServiceImpl implements SupplierService {

    @Autowired
    private SupplierMapper supplierMapper;
    @Override
    public PageVO findSupplierList(Integer pageNum, Integer pageSize, SupplierVO supplierVO) {
        PageHelper.startPage(pageNum,pageSize);
        Example example = new Example(Supplier.class);
        Example.Criteria criteria = example.createCriteria();

        @NotBlank(message = "地址不能为空") String address = supplierVO.getAddress();
        String contact = supplierVO.getContact();
        @NotBlank(message = "供应商名称不能为空") String name = supplierVO.getName();
        if (address!=null){
            criteria.andLike("address","%"+address+"%");
        }
        if (contact!=null){
            criteria.andLike("contact","%"+contact+"%");
        }
        if (name!=null){
            criteria.andLike("name","%"+name+"%");
        }

        List<Supplier> suppliers = supplierMapper.selectByExample(example);
        List<SupplierVO> supplierVOS = new ArrayList<>();
        for (Supplier supplier : suppliers) {
            SupplierVO supplierVO1 = new SupplierVO();
            BeanUtils.copyProperties(supplier,supplierVO1);
            supplierVOS.add(supplierVO1);
        }
        PageInfo pageInfo = new PageInfo<>(supplierVOS);
        PageVO pageVO = new PageVO(pageInfo.getTotal(),supplierVOS);
        return pageVO;
    }

    //添加
    @Override
    public ResponseBean add(SupplierVO supplierVO) {
        Supplier supplier = new Supplier();
        BeanUtils.copyProperties(supplierVO,supplier);
        supplier.setCreateTime(new Date());
        supplier.setModifiedTime(new Date());
        supplierMapper.insert(supplier);
        return ResponseBean.success();
    }


    // 用于回显数据
    public SupplierVO edit(Long id) {
        Supplier supplier = supplierMapper.selectByPrimaryKey(id);
        SupplierVO supplierVO = SupplierConverter.supplierToSupplierVo(supplier);
        return supplierVO;
    }

    //修改
    @Override
    public void update(Long id, SupplierVO supplierVO) {
        Supplier supplier = new Supplier();
        BeanUtils.copyProperties(supplierVO , supplier);
        supplier.setModifiedTime(new Date());
        supplierMapper.updateByPrimaryKey(supplier);
    }

    //删除
    @Override
    public void delete(Long id) {
        supplierMapper.deleteByPrimaryKey(id);
        supplierMapper.deleteByExample(id);
    }


}
